package viewer

import (
	"fmt"
	"gitee.com/alex_li/zangjingge/internal/tools"
	"github.com/lishimeng/app-starter"
	"github.com/lishimeng/app-starter/server"
	"math/rand/v2"
	"time"
)

type TokenResp struct {
	app.Response
	Token      string `json:"token"`
	HeaderName string `json:"headerName"`
	ParamName  string `json:"paramName"`
}

var RamTokenContainer = make(map[string]string)

func applyToken(ctx server.Context) {

	var resp TokenResp

	fileId := ctx.C.Params().Get("fileId")
	tokenContent := fmt.Sprintf("file_token_%s_%d_%d", fileId, time.Now().UnixNano(), rand.Int32())
	str := tools.GenMd5Code(tokenContent)
	RamTokenContainer[tokenContent] = fileId
	resp.Token = str

	resp.HeaderName = TokenHeader
	resp.ParamName = TokenParam
	ctx.Json(resp)
}
